<h1 id="自定义函数">自定义函数</h1>
<p>Quick中只有函数才会创建新的作用域</p>
<pre><code>函数名(函数形参) {
    函数体
}
</code></pre>
<p>Quick的匿名函数需要<code>$</code>来标识，非匿名的普通函数不需要<br>普通函数只需定义<code>函数名</code>，<code>函数形参</code>，<code>函数体</code>即可：</p>
<pre><code class="hljs language-javascript"><span class="hljs-title function_">add</span>(<span class="hljs-params">num1, num2</span>) {
    res = num1 + num2
    <span class="hljs-keyword">return</span> res
}
c = <span class="hljs-title function_">add</span>(<span class="hljs-number">3</span>*<span class="hljs-number">3</span>, <span class="hljs-number">4</span>*<span class="hljs-number">4</span>)
<span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;result:&quot;</span>, <span class="hljs-title function_">add</span>(<span class="hljs-number">5</span>, <span class="hljs-number">9</span>), c) <span class="hljs-comment">// result: 14 25</span>
</code></pre>
<p>Quick函数与js类似，实参数是可以比形参少的。<br>当你定义了形参，却不给它传值时，它的值为null:</p>
<pre><code class="hljs language-javascript"><span class="hljs-title function_">test</span>(<span class="hljs-params">a, b, c</span>) {
    <span class="hljs-title function_">printf</span>(<span class="hljs-string">&quot;a=%v, b=%v, c=%v\n&quot;</span>, a, b, c) <span class="hljs-comment">// a=fast, b=null, c=null</span>
}
<span class="hljs-title function_">test</span>(<span class="hljs-string">&quot;fast&quot;</span>)
</code></pre>
<p>函数定义完成可以立即调用，这样使用方式一般是想创建新的作用域才用</p>
<pre><code class="hljs language-javascript">${
    <span class="hljs-title function_">println</span>(<span class="hljs-string">&quot;It&#x27;s in anonymous function&quot;</span>)
}() <span class="hljs-comment">// It&#x27;s in anonymous function</span>

<span class="hljs-title function_">testx</span>(<span class="hljs-params">name</span>){
    <span class="hljs-title function_">printf</span>(<span class="hljs-string">&quot;It&#x27;s in %v function\n&quot;</span>, name)
}(<span class="hljs-string">&quot;normal&quot;</span>) <span class="hljs-comment">// It&#x27;s in normal function</span>
</code></pre>
